﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;


namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UAgentMacStockList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.删除, Permission.新增, Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UAgentMacStockList";
            }
        }
        #region 属性 (查询条件)





        protected string ShowAgentName
        {
            get
            {
                return GetRequestValue<string>("ShowAgentName", "");

            }
        }
        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }

       

        protected string IntoStockDateFrom
        {
            get
            {
                return GetRequestValue<string>("IntoStockDateFrom", "");

            }
        }

        protected string IntoStockDateTo
        {
            get
            {
                string temp = GetRequestValue<string>("IntoStockDateTo", "");
                if (string.IsNullOrEmpty(IntoStockDateFrom))
                    return "";
                if (string.IsNullOrEmpty(temp))
                {
                    temp= Convert.ToDateTime(IntoStockDateFrom).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
                }

                return temp;

            }
        }

        /// <summary>
        /// true表示只显示未交机的,否则显示全部
        /// </summary>
        protected bool IsInto
        {
            get
            {
                return GetRequestValue<bool>("IsInto", false);

            }
        }



        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("orderfield", "UASID");
            }
        }

        protected override string orderDirection
        {
            get
            {
                return GetRequestValue<string>("orderDirection", "asc");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
            this.AddLog(Permission.查看);
        }

        protected string GetSqlWhere()
        {
            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SerialNumber))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SerialNumber);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(ShowAgentName))
            {
                string xAgentName = ShowAgentName;
                xAgentName = "'" + xAgentName + "'";
                xAgentName = xAgentName.Replace(",", "','");

                sqlcommonFiled = string.Format("AgentName in ({0})", xAgentName);// QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, AgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }

            if (IsInto)
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IsInto", QueryOperatorsEnum.等于, "0");
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (IntoStockDateTo != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IntoStockDate", QueryOperatorsEnum.范围, IntoStockDateTo + "|" + IntoStockDateFrom);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
           

            return wheresql;
        }

        protected DataTable SUMDT = null;
        protected void DataBind()
        {
            DataTableCollection DTC = UAgentMacStockController.GetPagination(GetSqlWhere(), pageNum, numPerPage, SQLSort, ref TotalCount);
            this.rpMenu.DataSource = DTC[0];
            this.rpMenu.DataBind();
            SUMDT = DTC[1];
        }

        #region
        protected string GetAgentList()
        {
            DataTable AgentMacStockList = UAgentMacStockController.GetMacStockList();

            string litemp = "<li ><a tname=\"{7}\" tvalue=\"{1}\" title=\"全称:[{2}].公司地址:[{3}]\"  style=\"{4}\" {5}>{0}(<b style='color:red;'>{6}</b>)</a></li>";
            string listr = "";
            DataTable dt = StringHelper.GetDTToPinYin(SBOController.GetAgentList(), "AliasName", "PYAliasName", " Asc");
            foreach (DataRow dr in dt.Rows)
            {
                //string x = ShowAgentName == (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString()) ? "color:red;font-weight:bold;" : "";
                int pot = ShowAgentName.IndexOf(string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());
                string x = pot >= 0 ? "color:red;font-weight:bold;" : "";
                string check = pot >= 0 ? "checked='true'" : "";
                DataRow[] drs= AgentMacStockList.Select("agentname='" + (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]) + "'");
                string agstock="0";
                if(drs.Count()>0)
                {
                    agstock = drs[0]["macnum"].ToString();
                }
                string agentname = (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());

                listr += string.Format(litemp, dr["PYAliasName"].ToString() + agentname, dr["CardCode"], dr["CardName"], dr["Address"], x, check, agstock, agentname);
            }
            return listr;
        }

        protected string GetShowDetailStyle(object x)
        {
            string sHtml = x.ToString();
            string oHtml="";
            if (string.IsNullOrEmpty(sHtml))
                return "";
            sHtml=sHtml.Substring(2,sHtml.Length-2);
            string[] shtmlarr= sHtml.Split(new char[]{'|'});
            foreach(string item in shtmlarr)
            {
                string[] itemarr = item.Split(new char[] { ',' });
                oHtml += string.Format("<span class='UAgentMacStockList_tdspan'><b>{0}</b>:{1}</span>", itemarr[0], itemarr[1]);
            }
            return oHtml;
        }
        protected string GetNoSingNum(object d1,object d2)
        {
            if (d1 == DBNull.Value)
                return "";
            if (d2 == DBNull.Value)
                return "";
            return Convert.ToInt32(d1)==0?"0":(Convert.ToInt32(d1) - Convert.ToInt32(d2)).ToString();
        }

        protected object GetSumDt(string columnname)
        {
            if (SUMDT.Rows.Count <= 0)
                return 0;

            return SUMDT.Rows[0][columnname] == DBNull.Value ? "0" : SUMDT.Rows[0][columnname];
        }
        #endregion

        public string StringHelperagentname { get; set; }
    }
}